﻿
using Controller;
using HBase;
using hCommand;
using Net.http;
using Persistence;
using System;
using System.Collections.Generic; 

namespace WebApi {
    [FormAuthenticationFilter]

    [RoutePrefix("dict")]
    public class DictsController :BaseApiController {
        ISysAuthorityServer authorityServer = WMSDalFactray.getDal<ISysAuthorityServer>();


        [HttpPost, Route("FindPage")]
        public BaseResult FindPage([FromBody] DictsDto dto) {
            var info = authorityServer.QueryDictsPage(dto);
            return BaseResult.Ok(info);
        }

        [HttpPost, Route("Save"), ControlName("字典修改")]
        public BaseResult Save([FromBody] SysDict model) {
            if( String.IsNullOrWhiteSpace(model.dType) && model.pId != 0 ) {
                model.dType = authorityServer.getById<SysDict>(model.pId).dType;
            }

            if( model.id == 0 ) { 
                authorityServer.insert(model);
            } else {

               authorityServer.update(model);
            }
            MyCache.setDirty<DictRes>();
            TaskPool.doWork<CacheSyncTask>();
            return BaseResult.Ok("成功");
        }

        [HttpPost, Route("Delete"), ControlName("字典删除")]
        public BaseResult Delete([FromBody] List<SysDict> modelList) {
            foreach( var item in modelList ) {
                authorityServer.delete<SysDict>(item.id);
            }
            MyCache.setDirty<DictRes>();
            TaskPool.doWork<CacheSyncTask>();
            return BaseResult.Ok("1");
        }


        [HttpPost, Route("GetAllDicts")]
        public BaseResult GetAllDicts() {
            var dicts = MyCache.getList<DictRes>();
            return BaseResult.Ok(dicts);
        }

        [HttpGet, Route("getDictTree")]
        public BaseResult getDictTree(String name) {

            List<DictRes> listsrc = MyCache.getList<DictRes>();
            List<DictRes> res = new List<DictRes>();

            List<DictRes> list = new List<DictRes>();
            foreach( DictRes r in listsrc ) {
                DictRes rr = ( DictRes ) r.Clone();
                rr.children = new List<DictRes>();
                list.Add(rr);
            }
            foreach( DictRes r in list ) {
                if( r.pId == 0 ) {
                    res.Add(r);
                } else {
                    for( int i = 0;i < list.Count;i++ ) {
                        if( list[i].id == r.pId ) {
                            list[i].children.Add(r);
                            break;
                        }
                    }

                }

            }
            if( !string.IsNullOrEmpty(name) )
                remove(res ,name);

            return BaseResult.Ok(res);
        }

        bool remove(List<DictRes> list ,String name) {

            if( list.Count == 0 ) {
                return false;
            }

            bool hasChild = false;
            for( int i = 0;i < list.Count;i++ ) {
                DictRes res = list[i];
                bool has = remove(res.children ,name);
                if( has ) {
                    hasChild = true;
                    continue;
                }
                if( res.label.IndexOf(name) == -1 ) {
                    list.RemoveAt(i);
                    i--;
                } else {
                    hasChild = true;

                }
            }

            return hasChild;



        }



    }
}